package com.fjh.springboot_01.service.impl;

import com.fjh.springboot_01.mapper.FriendMapper;
import com.fjh.springboot_01.mapper.UserMapper;
import com.fjh.springboot_01.pojo.Friend;
import com.fjh.springboot_01.pojo.Result;
import com.fjh.springboot_01.pojo.User;
import com.fjh.springboot_01.service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
public class FriendServiceImpl implements FriendService {

    @Autowired
    private FriendMapper friendMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result addFriendRequest(int userId, int friendId) {
        // 检查用户是否尝试添加自己为好友
        if (userId == friendId) {
            return Result.error("不能添加自己为好友");
        }

        // 检查是否已存在好友请求
        Friend existingFriendship = friendMapper.findFriendshipStatus(userId, friendId);
        if (existingFriendship != null) {
            return Result.error("好友请求已存在");
        }

        // 获取发送者用户名
        User sender = userMapper.findById(userId);
        String message = sender.getUsername() + " 向您发送了好友请求";
        
        friendMapper.addFriendRequest(userId, friendId, message);
        return Result.success("好友请求已发送");
    }

    @Override
    public void saveNotification(int userId, int friendId) {
        // 通知已经在addFriendRequest中保存了
        // 这个方法保留用于未来可能的扩展
    }

    @Override
    public Result getNotifications(int userId) {
        try {
            List<Friend> notifications = friendMapper.getUnhandledNotifications(userId);
            return Result.success(notifications);
        } catch (Exception e) {
            return Result.error("获取通知失败：" + e.getMessage());
        }
    }

    @Override
    public Result markNotificationAsRead(int notificationId) {
        try {
            friendMapper.markNotificationAsRead(notificationId);
            return Result.success("通知已标记为已读");
        } catch (Exception e) {
            return Result.error("标记通知失败：" + e.getMessage());
        }
    }

    @Override
    public Result acceptFriendRequest(int userId, int friendId) {
        // 检查是否有待接受的好友请求
        Friend existingFriendship = friendMapper.findFriendshipStatus(userId, friendId);
        if (existingFriendship == null || existingFriendship.getStatus() != 0) {  // 这里比较 int 类型，正常使用
            return Result.error("没有待接受的好友请求");
        }

        friendMapper.acceptFriendRequest(userId, friendId);
        return Result.success("好友请求已接受");
    }

    @Override
    public Result rejectFriendRequest(int userId, int friendId) {
        // 检查是否有待拒绝的好友请求
        Friend existingFriendship = friendMapper.findFriendshipStatus(userId, friendId);
        if (existingFriendship == null || existingFriendship.getStatus() != 0) {  // 这里比较 int 类型，正常使用
            return Result.error("没有待拒绝的好友请求");
        }

        friendMapper.rejectFriendRequest(userId, friendId);
        return Result.success("好友请求已拒绝");
    }

    @Override
    public Result getFriendList(int userId) {
        // 获取用户的好友列表
        List<User> friends = friendMapper.findUserFriends(userId);
        return Result.success(friends);
    }

    @Override
    public Result deleteFriend(int userId, int friendId) {
        // 校验是否为自己
        if (userId == friendId) {
            return Result.error("不能删除自己");
        }

        // 校验好友关系存在性
        Friend relationship = friendMapper.findFriendshipStatus(userId,  friendId);
        if (relationship == null || relationship.getStatus()  != 1) {
            return Result.error("好友关系不存在");
        }

        // 执行删除操作
        friendMapper.deleteFriend(userId,  friendId);
        return Result.success("好友删除成功");
    }

}
